package com.xunwulian.cmeb.command;

import com.xunwulian.base.constant.ConstantBean;
import com.xunwulian.base.exception.ErrorCodeException;
import org.json.JSONArray;

public class BalanceCommand extends Command {

    String msisdn = "";

    public BalanceCommand(Receiver receiver, String msisdn) {
        super(APICode.BALANCE, receiver);
        this.msisdn = msisdn;
    }

    @Override
    public String getParam() {
        return "msisdn=" + msisdn;
    }

    /**
     * Fail: ConstantBean.FAIL_VALUE_INT: -1
     *
     * @return
     */
    @Override
    public Double execute() throws ErrorCodeException {
        JSONArray result = receiver.actionPost(api, getParam());

        double balance = ConstantBean.FAIL_VALUE_DOUBLE;
        if (null != result && result.length() != 0) {
            balance = Double.valueOf(result.getJSONObject(0).getString("balance"));
        }
        return balance;
    }
}
